package com.dragon.www.haier.popupwindow;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.dragon.www.haier.R;
import com.dragon.www.haier.activity.MainActivity;
import com.dragon.www.haier.adapter.ConferenceAdapter;
import com.dragon.www.haier.entity.Device;
import com.dragon.www.haier.view.dialog.DeviceEditDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by daiye on 2016/6/1.
 */
public class ConferencePopWindow extends BasePopupWindow implements AdapterView.OnItemClickListener{
    private final static int ConferencePopBackgroundColor = 0xD9FF7200;
    private ImageView imageBackConference;
    private ImageButton buttonBack;
    private ImageButton buttonEdit;
    private ImageButton buttonDelete;
    private ImageButton buttonChange;
    private ImageButton buttonAllDeviceOn;
    private ImageButton buttonAllDeviceOff;

    private List<Device> devices = new ArrayList<>();
    private ConferenceAdapter adapter;
    private GridView gview;

    private DeviceEditDialog mDeviceEditDialog;

    private boolean bDeviceEditMode = false;
    private HashMap<String, Object> mHasIntoEditModeMap = new HashMap<String, Object>();

    public ConferencePopWindow(Context context) {
        super(context);
    }

    public void showPopWindow(View view, int xOffsetToView, int yOffsetToView) {
        contentView = LayoutInflater.from(context).inflate(R.layout.conference_view, null);
        super.showPopWindow(view, xOffsetToView, yOffsetToView);
        imageBackConference = (ImageView) contentView.findViewById(R.id.image_back_conference);
        buttonBack = (ImageButton) contentView.findViewById(R.id.bt_back);
        buttonEdit = (ImageButton) contentView.findViewById(R.id.bt_edit);
        buttonDelete = (ImageButton) contentView.findViewById(R.id.bt_delete);
        buttonChange = (ImageButton) contentView.findViewById(R.id.bt_change);
        buttonAllDeviceOn = (ImageButton) contentView.findViewById(R.id.bt_all_on);
        buttonAllDeviceOff = (ImageButton) contentView.findViewById(R.id.bt_all_off);

        gview = (GridView) contentView.findViewById(R.id.gview);
        adapter = new ConferenceAdapter(context);
        gview.setOnItemClickListener(this);
        gview.setAdapter(adapter);

        imageBackConference.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        buttonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bDeviceEditMode) {
                    //退出编辑模式
                    bDeviceEditMode = false;
                    mHasIntoEditModeMap.clear();
                    buttonEdit.setVisibility(View.VISIBLE);
                    buttonAllDeviceOn.setVisibility(View.VISIBLE);
                    buttonAllDeviceOff.setVisibility(View.VISIBLE);
                    buttonDelete.setVisibility(View.GONE);
                    buttonChange.setVisibility(View.GONE);
                    refreshData();
                } else {
                    //退出会议设备管理窗口
                    dismiss();
                }
            }
        });
        buttonEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入会议设备编辑模式
                bDeviceEditMode = true;
                buttonEdit.setVisibility(View.GONE);
                buttonAllDeviceOn.setVisibility(View.GONE);
                buttonAllDeviceOff.setVisibility(View.GONE);
                buttonDelete.setVisibility(View.GONE);
                buttonChange.setVisibility(View.GONE);
                refreshData();
            }
        });
        buttonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bDeviceEditMode) {
                    //删除设备 并 退出编辑模式
                    List<Device> deleteDevices = new ArrayList<>();
                    for (String key : mHasIntoEditModeMap.keySet()) {
                        int deletePosition = Integer.valueOf((String) mHasIntoEditModeMap.get(key)).intValue();
                        Device deleteDevice = devices.get(deletePosition);
                        deleteDevices.add(deleteDevice);
                    }
                    for(int i = 0; i < deleteDevices.size(); i++){
                        Device dev = deleteDevices.get(i);
                        devices.remove(dev);
                    }

                    bDeviceEditMode = false;
                    mHasIntoEditModeMap.clear();
                    buttonEdit.setVisibility(View.VISIBLE);
                    buttonAllDeviceOn.setVisibility(View.VISIBLE);
                    buttonAllDeviceOff.setVisibility(View.VISIBLE);
                    buttonDelete.setVisibility(View.GONE);
                    buttonChange.setVisibility(View.GONE);
                    refreshData();
                }
            }
        });
        buttonChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bDeviceEditMode) {
                    //修改设备 并 退出编辑模式
                    for (String key : mHasIntoEditModeMap.keySet()) {
                        int changePosition = Integer.valueOf((String) mHasIntoEditModeMap.get(key)).intValue();
                        Device changeRoom = devices.get(changePosition);

                    }

                    bDeviceEditMode = false;
                    mHasIntoEditModeMap.clear();
                    buttonEdit.setVisibility(View.VISIBLE);
                    buttonAllDeviceOn.setVisibility(View.VISIBLE);
                    buttonAllDeviceOff.setVisibility(View.VISIBLE);
                    buttonDelete.setVisibility(View.GONE);
                    buttonChange.setVisibility(View.GONE);
                    refreshData();
                }
            }
        });
        buttonAllDeviceOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                turnAllDeviceOn();
            }
        });
        buttonAllDeviceOff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                turnAllDeviceOff();
            }
        });

        //防黑边
        this.setBackgroundDrawable(new BitmapDrawable());
        //显示
        GradientDrawable myGrad = (GradientDrawable) contentView.getBackground();
        myGrad.setColor(ConferencePopBackgroundColor);

        refreshData();
    }

    private void refreshData(){
        adapter.setDeviceEditEnable(bDeviceEditMode);
        adapter.setDeviceEditMap(mHasIntoEditModeMap);
        adapter.setData(devices);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (!bDeviceEditMode) {
            //如果在非编辑模式执行新增设备的操作
            if ( position == devices.size() ) {
                showAddDeviceDialog();
            }
        } else {
            // 如果在编辑模式的话执行选中删除操作
            if ( position < devices.size() ) {
                if (mHasIntoEditModeMap.containsKey(String.valueOf(position))) {
                    mHasIntoEditModeMap.remove(String.valueOf(position));
                } else {
                    mHasIntoEditModeMap.put(String.valueOf(position), String.valueOf(position));
                }
                //根据不同的
                if (mHasIntoEditModeMap.size() > 1) {
                    buttonDelete.setVisibility(View.VISIBLE);
                    buttonChange.setVisibility(View.GONE);
                } else if (mHasIntoEditModeMap.size() == 1) {
                    buttonDelete.setVisibility(View.VISIBLE);
                    buttonChange.setVisibility(View.VISIBLE);
                } else if (mHasIntoEditModeMap.size() < 1) {
                    buttonDelete.setVisibility(View.VISIBLE);
                    buttonChange.setVisibility(View.GONE);
                }
                refreshData();
            }
        }
    }

    private boolean isTypeContain(String type){
        boolean is = false;
        for(int i = 0; i < devices.size(); i++)
        {
            if( devices.get(i).getType() == type )
            {
                is = true;
                continue;
            }
        }
        return is;
    }

    private Device getTypeDevice(String type){
        Device device = new Device();
        device.setDeviceName("");
        device.setDeviceId(-1);
        device.setType(type);
        return device;
    }

    public void showAddDeviceDialog() {
        mDeviceEditDialog = new DeviceEditDialog(context);
        mDeviceEditDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDeviceEditDialog.show();
        mDeviceEditDialog.setLisntener(new DeviceEditDialog.OnSelectListener() {
            @Override
            public void addLamp() {
                if(!isTypeContain(MainActivity.TYPE_LAMP)){
                    devices.add(getTypeDevice(MainActivity.TYPE_LAMP));
                    refreshData();
                }else{
                    Toast.makeText(context, "已经添加过灯光设备了，不能重复添加！", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void addAir() {
                if(!isTypeContain(MainActivity.TYPE_AIR)){
                    devices.add(getTypeDevice(MainActivity.TYPE_AIR));
                    refreshData();
                }else{
                    Toast.makeText(context, "已经添加过空调设备了，不能重复添加！", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void addCurtain() {
                if(!isTypeContain(MainActivity.TYPE_CURTAIN)){
                    devices.add(getTypeDevice(MainActivity.TYPE_CURTAIN));
                    refreshData();
                }else{
                    Toast.makeText(context, "已经添加过窗帘设备了，不能重复添加！", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void addEntranceguard() {
                if(!isTypeContain(MainActivity.TYPE_ENTANCEGUARD)){
                    devices.add(getTypeDevice(MainActivity.TYPE_ENTANCEGUARD));
                    refreshData();
                }else{
                    Toast.makeText(context, "已经添加过门禁设备了，不能重复添加！", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void addHumidifier() {
                if(!isTypeContain(MainActivity.TYPE_HUMIDIFIER)){
                    devices.add(getTypeDevice(MainActivity.TYPE_HUMIDIFIER));
                    refreshData();
                }else{
                    Toast.makeText(context, "已经添加过加湿器设备了，不能重复添加！", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void addPlantwall() {
                if(!isTypeContain(MainActivity.TYPE_PLANTWALL)){
                    devices.add(getTypeDevice(MainActivity.TYPE_PLANTWALL));
                    refreshData();
                }else{
                    Toast.makeText(context, "已经添加过植物墙设备了，不能重复添加！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void turnAllDeviceOn(){
        for(int i = 0; i < devices.size(); i++)
        {



        }
    }

    private void turnAllDeviceOff(){
        for(int i = 0; i < devices.size(); i++)
        {



        }
    }
}
